//狗狗控制脚本
cc.Class({
    extends: cc.Component,

    properties: {
        //doge动作状态
        doge_action: {
            default: "",
            visible: false
        },
        //doge动作帧
        action_frame: {
            default: [],
            type: cc.SpriteFrame
        },
        //doge当前目标位置
        doge_arrive:{
            default: cc.p(0,0),
            visible: false
        },
        //doge当前移动角度与方向
        doge_deviation: {
            default: null,
            visible: false
        },
        //doge上一次位置
        doge_last: {
            default: cc.p(0,0),
            visible: false
        },
        //doge的上一次状态
        last_state: {
            default: "",
            visible: false
        },
        //doge移动速度
        doge_speed: {
            default: 0,
            visible: false
        },
        //自身类型
        doge_type: "yellow",
        test: 0
    },

    onLoad () {
        this.interval = 0;
        this.last_state = "walk";
        //this.modfilys_node("ready","blue")
    },

    update (dt) {
        if(Game.State == GameState.End) {
            return;
        }
        this.Check_Collision();
        this.Check_State(dt);
    },

    //初始化doge状态
    //init_ob:初始化对象
    init_state (init_ob) {
        this.doge_action = init_ob.action;
        this.doge_arrive = init_ob.arrive;
        this.doge_deviation = init_ob.deviation;
        this.last_state = "walk";
    },
    
    //停止狗狗动画
    stop_anim () {
        var animation = this.node.getComponent(cc.Animation);
        animation.stop();
    },

    //重置狗狗对象
    reset_doge() {
        var anim = this.node.getComponent(cc.Animation);
        anim.play();  //恢复行走动画
        this.last_state = "walk";
        this.modfilys_node("walk",this.doge_type);
    },

    //doge状态检测
    Check_State (dt) {
        //ready状态0.8秒后切换push
        if(this.doge_action == "ready") {
            this.interval += dt;
            if(parseFloat(this.interval.toFixed(2)) >= 0.8) {
                this.doge_action = "push";
                this.interval = 0;
            }
        }
        //dying状态0.5秒后切换die
        if(this.doge_action == "dying") {
            this.interval += dt;
            if(parseFloat(this.interval.toFixed(2)) >= 0.5) {
                this.doge_action = "die";
                this.modfilys_node("die",this.doge_type);
                this.interval = 0;
            }
        }
        if(this.doge_action == "walk" || this.doge_action == "push") {
            var speed = parseFloat((this.doge_speed * dt).toFixed(2));
            //若当前与猫猫的距离小于100,则准备扑咬
            if(this.distance_cat() <= 100 && this.last_state == "walk") {
                this.stop_anim();
                this.last_state = "ready";
                this.modfilys_node("ready",this.doge_type);  //切换为扑咬姿态
                return;
            }
            //若当前状态为push则运动速度增加
            if(this.doge_action == "push") {
                if(this.last_state == "ready") {
                    this.last_state = "push";
                    this.modfilys_node("push",this.doge_type);
                }
                speed = speed + parseFloat((100 * dt).toFixed(2));
            }
            this.doge_last = this.doge_move(dt,speed);
            //若doge未到达屏幕边界则进行移动,若到达则强制恢复初始状态
            if(Commond.Check_Screen(Game.Screen,this.node) == false) {
                if(Commond.Check_Arrive(this.node,this.doge_arrive) == true) {
                    this.reset_doge();
                }
            }else {
                //doge节点出界则退回之前位置[避免陷入死循环]
                this.node.x = this.doge_last.x;
                this.node.y = this.doge_last.y;
                this.reset_doge();
            }
        }
    },

    //判断与猫猫节点碰撞情况
    Check_Collision () {
        var cat = Game.Cat.getComponent("cat_self");
        var self = this.node.getPosition();
        var distan = Commond.Distance_Point(Game.Cat.getPosition(),self);
        this.test = distan;
        if(this.doge_action == "die") {return}
        if(distan < 20 && cat.cat_action == "kick") {
            if(this.doge_action != "dying") {
                if(this.doge_type == "blue") {
                    Game.Score += 2;
                }else {
                    Game.Score += 1;
                }
                Game.Count += 1;
                this.doge_action = "dying";
                this.modfilys_node("dying",this.doge_type);
                Game.Self.getComponent("game").update_score(Game.Score);
            }
            return;
        }
        if(distan < 20 && cat.cat_action == "breath") {
            if(this.doge_action == "push") {
              cat.getComponent(cc.Animation).stop();
              cat.modfily_node("die");
            }
            return;
        }

    },

    //根据动作类型修改节点大小
    //[a_type:动作类型 d_type:狗狗类型]
    modfilys_node (a_type,d_type) {
        var doge = this.node;
        var width = 0;
        var height = 0;
        var dogeframe = null;
        var frame = null;
        switch(d_type) {
            case "blue":
                dogeframe = Blue_Doge;
            break;
            case "yellow":
                dogeframe = Yellow_Doge;
            break;
        }
        switch(a_type) {
            case "ready": //准备
                width = 62;
                height = 30;
                frame = dogeframe.READY;
            break;
            case "walk":  //走
                width = 46;
                height = 38;
            break;
            case "die":  //扑街
                width = 66;
                height = 28;
                frame = dogeframe.DIE;
            break;
            case "dying":  //濒死
                width = 50;
                height = 38;
                frame = dogeframe.DYING;
            break;
            case "push":  //扑咬
                width = 58;
                height = 40;
                frame = dogeframe.PUSH;
            break;
        }
        doge.width = width;
        doge.height = height;
        this.doge_action = a_type;
        Commond.Change_Frame(doge,this.action_frame[frame]);
    },

    //doge运动方法
    //dt:移动间隔 speed:速度
    doge_move (dt,speed) {
        var unix = cc.p(0,0);
        var last = this.node.getPosition();  //获取移动前的猫猫位置
        var angel = Math.abs(this.doge_deviation.angel);
        switch(this.doge_deviation.dir) {
            case "RT":
                angel = angel;
            break;
            case "LT":
                angel = 180 - angel;
            break;
            case "LB":
                angel = 180 + angel;
            break;
            case "RB":
                angel = 360 - angel;
            break;
        }
        unix = Commond.Return_Vector(angel,speed);
        this.node.x += unix.x;
        this.node.y += unix.y;
        return last;
    },

    //doge与猫猫的距离判定
    distance_cat () {
        var arrive = Game.Cat.getPosition();
        var self = this.node.getPosition();
        var distan = Commond.Distance_Point(self,arrive);
        return parseInt(distan);
    }
});
